var GlobalErrorListener = require('./GlobalErrorListener');
Object.prototype.hasOwnProperty;
var Event = function () {
};
Event.prototype = Object.create(Object.prototype, {
    constructor: {
        value: Event,
        writable: true,
        configurable: true
    }
});
var r = null;
Event._setElementSystem = function (e) {
    r = e;
    Event._setElementSystem = null;
};
var o = null;
Event._setComponent = function (e) {
    o = e;
    Event._setComponent = null;
};
var s = Date.now();
Event.setInitTimeStamp = function (e) {
    return s = undefined === e ? Date.now() : e;
};
Event.getInitTimeStamp = function (e) {
    return s;
};
Event.create = function (e, t, i) {
    var n = (i = i || {}).originalEvent;
    var r = i.extraFields || {};
    var o = Date.now() - s;
    var l = new Event();
    for (var d in (l.currentTarget = null, l.type = e, l.timeStamp = o, l.mark = null, l.detail = t, l.bubbles = !!i.bubbles, l.composed = !!i.composed, l.__originalEvent = n, l.__hasCapture = !!i.capturePhase, l.__stopped = false, l.__dispatched = false, r))
        l[d] = r[d];
    return l;
};
Event.prototype.preventDefault = function () {
    this.__originalEvent && !this.__originalEvent.__stopPreventDefault && this.__originalEvent.preventDefault();
};
Event.prototype.stopPropagation = function () {
    this.__stopped = true;
};
Event.prototype.isStopped = function () {
    return !!this.__stopped;
};
Event.prototype.markMutated = function () {
    this.__mutatedMarked = true;
};
Event.prototype.mutatedMarked = function () {
    return !!this.__mutatedMarked;
};
var l = function (e, t, i, n) {
    var a = e;
    var o = e instanceof r ? e.collectMarks() : {};
    var s = [];
    var l = [];
    for (var d = e; d;) {
        if (a !== d) {
            s.push(e);
            e = d;
            l.push(o);
            o = e instanceof r ? e.collectMarks() : {};
        };
        a = d.parentNode;
        if (false === n(d, e, o))
            return;
        if (d.__wxHost) {
            if (i)
                break;
            e = s.pop() || d.__wxHost;
            o = l.pop() || (e instanceof r ? e.collectMarks() : {});
            a = d = d.__wxHost;
        } else {
            var A = true;
            d instanceof r && (A = false);
            d = A || i ? d.parentNode : d.__wxSlotParent;
        }
    }
};
Event.dispatchEvent = function (e, t) {
    if (!t.__dispatched) {
        t.__dispatched = true;
        e.__wxElement && e.__wxHost !== e.__wxElement && (e = e.__wxElement).shadowRoot instanceof r && (e = e.shadowRoot);
        t.target = e instanceof r ? e.__methodCaller : e;
        var i = function (e, i, n) {
            if (!(i && i.length && t.mutatedMarked())) {
                var a = t.currentTarget = n instanceof r ? n.__methodCaller : n;
                var s = e.call(a, [t], n instanceof o ? n : undefined);
                i && i.length && t.markMutated();
                false === s && (t.__originalEvent && !t.__originalEvent.__stopPreventDefault && t.__originalEvent.preventDefault(), t.__stopped = true);
            }
        };
        var n = t.type;
        var a = !t.composed;
        if (t.__hasCapture) {
            var s = [];
            l(e, 0, a, function (e, t, i) {
                e.__wxCaptureEvents && e.__wxCaptureEvents[n] && s.push([
                    e,
                    t,
                    i
                ]);
                return true;
            });
            for (var d = s.length - 1; d >= 0; d--) {
                var A = s[d];
                var c = A[0];
                var h = A[1];
                t.target = h instanceof r ? h.__methodCaller : h;
                t.mark = A[2];
                var u = c.__wxCaptureMutated && c.__wxCaptureMutated[n];
                i(c.__wxCaptureEvents[n], u, c);
                if (t.__stopped)
                    break;
            }
        }
        t.target = e instanceof r ? e.__methodCaller : e;
        if (!t.__stopped) {
            var p = !t.bubbles;
            l(e, 0, a, function (e, a, o) {
                t.target = a instanceof r ? a.__methodCaller : a;
                t.mark = o;
                var s = e.__wxMutated && e.__wxMutated[n];
                e.__wxEvents && e.__wxEvents[n] && i(e.__wxEvents[n], s, e);
                return !p && !t.__stopped;
            });
        }
    }
};
Event.triggerEvent = function (e, t, i, n) {
    var r = Event.create(t, i, n);
    Event.dispatchEvent(e, r);
};
Event.addListenerToElement = function (e, t, i, a) {
    var r = a && a.mutated;
    if (r) {
        var s = i;
        i = function (e) {
            if (!e.mutatedMarked()) {
                var t = Array.prototype.slice.call(arguments);
                return s.apply(this, t);
            }
        };
    }
    var l = undefined;
    if (a && (a.useCapture || a.capture)) {
        e.__wxCaptureEvents || (e.__wxCaptureEvents = Object.create(null));
        e.__wxCaptureEvents[t] || (e.__wxCaptureEvents[t] = GlobalErrorListener.create('Event Listener'));
        l = e.__wxCaptureEvents[t].add(i);
        if (r) {
            e.__wxCaptureMutated || (e.__wxCaptureMutated = Object.create(null));
            e.__wxCaptureMutated[t] || (e.__wxCaptureMutated[t] = []);
            e.__wxCaptureMutated[t].push(l);
        };
    } else {
        e.__wxEvents || (e.__wxEvents = Object.create(null));
        e.__wxEvents[t] || (e.__wxEvents[t] = GlobalErrorListener.create('Event Listener'));
        l = e.__wxEvents[t].add(i);
        if (r) {
            e.__wxMutated || (e.__wxMutated = Object.create(null));
            e.__wxMutated[t] || (e.__wxMutated[t] = []);
            e.__wxMutated[t].push(l);
        };
    }
    e instanceof o && e.__componentOptions.listenerChangeLifeTimes && e.triggerLifeTime('listenerChanged', [
        true,
        t,
        i,
        a
    ]);
    return l;
};
Event.removeListenerFromElement = function (e, t, i, n) {
    var a;
    var r = null;
    if (n && (n.useCapture || n.capture)) {
        e.__wxCaptureEvents && e.__wxCaptureEvents[t] && (r = e.__wxCaptureEvents[t].remove(i));
        e.__wxCaptureMutated && e.__wxCaptureMutated[t] && -1 !== (a = e.__wxCaptureMutated[t].indexOf(i)) && e.__wxCaptureMutated[t].splice(a, 1);
    } else {
        e.__wxEvents && e.__wxEvents[t] && (r = e.__wxEvents[t].remove(i));
        e.__wxMutated && e.__wxMutated[t] && -1 !== (a = e.__wxMutated[t].indexOf(i)) && e.__wxMutated[t].splice(a, 1);
    }
    r && e instanceof o && e.__componentOptions.listenerChangeLifeTimes && e.triggerLifeTime('listenerChanged', [
        false,
        t,
        r,
        n
    ]);
};
module.exports = Event;
